<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <!-- import CSS -->
    <link rel="stylesheet" href="https://cdn.staticfile.org/element-ui/2.15.9/theme-chalk/index.css">
    <!--引入富文本编辑器css-->
    <!--    导入自己的css文件-->
    <link rel="stylesheet" href="../css/my-css.css">

    <style>
        .center{
            width: 1200px;
            margin: 0 auto;
        }
        #footer_center p{
            margin: 0;
        }
        #footer_center h3{
            color: white;
        }
        body{
            background-color: rgb(241,242,243);
        }
        .el-header{
            background-color: white;
        }
    </style>
</head>
<body>
<div id="app" >
    <el-container>
        <my-header></my-header>
        <el-main class="center" style="width: 1200px; height: 600px; margin: 0 auto;padding-left: 125px">
            <el-row gutter="20">
                <el-col span="4">
                    <el-card style="height: 500px">
                        <p @click="location.href='../usermodel/personal.html'" style="margin-bottom: 30px;cursor: pointer">个人信息</p>
                        <p @click="location.href='../notmodel/postNote.html'" style="margin-bottom: 30px;cursor: pointer">笔记管理</p>
                        <p style="color: #409EFF">视频管理</p>
                    </el-card>
                </el-col>
                <el-col span="20">
                    <el-card>
                        <!--发布文章表单开始-->
                        <el-form label-width="100px">
                            <el-form-item label="视频标题">
                                <el-input style="width: 200px" v-model="s.title"
                                          placeholder="请输入标题"></el-input>
                            </el-form-item>
                            <el-form-item label="科目分类">
                                <el-select v-model="s.subjectId">
                                    <el-option v-for="sub in subjectArr"
                                               :label="sub.name" :value="sub.id"></el-option>
                                </el-select>
                            </el-form-item>
                            <el-form-item label="封面">
                                <img v-if="isEdit" :src="oldImgUrl"
                                     style="width: 145px;height: 145px;">
                                <el-upload
                                        action="/v1/upload"
                                        name="file"
                                        limit="1"
                                        list-type="picture-card"
                                        :on-success="handleSuccess"
                                        :on-preview="handlePictureCardPreview"
                                        :on-remove="handleRemove">
                                    <i class="el-icon-plus"></i>
                                </el-upload>
                                <el-dialog :visible.sync="dialogVisible">
                                    <img width="100%" :src="dialogImageUrl" alt="">
                                </el-dialog>
                            </el-form-item>
                            <el-form-item  label="视频">
                                <video v-if="isEdit" :src="c.videoUrl"
                                       muted autoplay controls style="width: 200px; height: 150px;">
                                    您的浏览器不支持播放此视频.
                                </video>
                                <el-upload style="width: 300px"
                                           action="/v1/upload"
                                           accept="video/mp4,video/x-m4v,video/*"
                                           name="file"
                                           limit="1"
                                           :on-success="handleSuccess"
                                           :on-preview="handlePictureCardPreview"
                                           :on-remove="handleRemove">
                                    <el-button type="primary">点击上传</el-button>
                                    <div>只能上传mp4文件,且不超过100MB</div>
                                </el-upload>
                            </el-form-item>
                            <el-form-item label="视频摘要">
                                <!--<div style="border: 1px solid #ccc">
                                    &lt;!&ndash;<div id="toolbar-container" style="border-bottom: 1px solid #ccc;">&lt;!&ndash; 工具栏 &ndash;&gt;</div>
                                    <div id="editor-container" style="height: 300px;">&lt;!&ndash; 编辑器 &ndash;&gt;</div>&ndash;&gt;
                                </div>-->
                                <el-input style="width: 808px;" v-model="s.brief"></el-input>
                            </el-form-item>
                            <el-form-item  style="float: left">
                                <el-button type="primary" @click="post()">{{isEdit?"修改":"发布"}}</el-button>
                            </el-form-item>
                            <el-form-item  style="float: left">
                                <el-button type="primary" @click="location.href='../videomodel/videoManagement.html'">返回</el-button>
                            </el-form-item>
                        </el-form>

                        <!--发布文章表单结束-->

                    </el-card>
                </el-col>
            </el-row>
        </el-main>
        <my-footer></my-footer>
    </el-container>
</div>
</body>

<!-- import Vue before Element -->
<script src="https://cdn.staticfile.org/vue/2.6.14/vue.min.js"></script>
<!-- import JavaScript -->
<script src="https://cdn.staticfile.org/element-ui/2.15.9/index.min.js"></script>
<!--引入富文本编辑器js-->
<!--引入Axios-->
<script src="https://cdn.bootcss.com/axios/0.18.0/axios.min.js"></script>
<!--导入自己的头部js文件-->
<script src="../js/my-header.js"></script>
<!--导入自己的尾部js文件-->
<script src="../js/my-footer.js"></script>

<script>
    let v = new Vue({
        el: '#app',
        data: function () {
            return {
                oldImgUrl:"",
                isEdit:false,
                dialogImageUrl: '',
                dialogVisible: false,
                subjectArr:[],
                s:{
                    title:"", cover:"",brief:"",videoUrl:"",subjectId:""/*,content:""*/
                }
            }
        },
        methods: {
            post(){
                if (v.s.videoUrl==""){
                    v.$message.error("请选择视频文件!");
                    return;
                }
                if (v.s.cover==""){
                    v.$message.error("请选择一张封面图!");
                    return;
                }
                if (v.s.subjectId==null||v.s.subjectId==""){
                    v.$message.error("请选择分类!");
                    return;
                }
                //设置作者id
                //v.s.createBy = JSON.parse(localStorage.user).id;
                //设置摘要  slice(起始位置,结束位置)
                //v.s.brief = v.s.content.slice(0,50);
                //发出请求
                axios.post("/v1/video/insert",v.s).then(function (response) {
                    if (response.data.code==20000){
                        v.$message.success("发布完成!");
                        location.href="/videomodel/videoManagement.html";
                    }
                })
            },
            handleSuccess(response, file, fileList) {
                console.log(file);
                if (file.raw.type.includes("image")){
                    //把上传图片完成之后 得到的图片路径用变量记录
                    v.s.cover = response.data;
                }else{
                    v.s.videoUrl = response.data;
                }
            },
            handleRemove(file, fileList) {
                //判断删除的是图片还是视频
                if (file.raw.type.includes("image")){
                    v.s.cover="";
                }else{
                    v.s.videoUrl = "";
                }
                //发出删除文件的请求   file.response上传成功时服务器响应的内容ResultVO
                console.log(file.response);
                axios.get("/v1/upload/remove?url=" + file.response.data).then(function () {
                    console.log("服务器文件删除完成!");
                })
            },
            handlePictureCardPreview(file) {
                this.dialogImageUrl = file.url;
                this.dialogVisible = true;
            }
        },
        created:function () {
            //请求文章类型下的所有分类
            axios.get("/v1/subject/selectSubject").then(function (response) {
                if (response.data.code==20000){
                    v.subjectArr = response.data.data;
                }
            })
        }
    })

</script>
</html>